package com.cuitrip.business.pay;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import com.cuitrip.business.login.LoginInstance;
import com.cuitrip.business.order.detail.OrderFormActivity;
import com.cuitrip.business.order.model.OrderItem;
import com.cuitrip.business.user.model.CtUserInfo;
import com.cuitrip.im.CtInfoNotificationMsgItemProvider;
import com.cuitrip.im.model.CTInformationNotificationMessage;
import com.cuitrip.im.proxy.CtRongInfoProxy;
import com.cuitrip.service.R;
import com.cuitrip.util.time.InputTime;
import com.cuitrip.util.time.OutputTime;
import com.cuitrip.util.time.TimeUtil;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragmentActivity;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by baziii on 15/8/17.
 */
public class PaySucActivity extends CustomUiFragmentActivity {
    @Bind(R.id.back_tv)
    TextView backTv;

    @Bind(R.id.pay_suc_desc)
    TextView paySucDesc;

    public static final String ORDER_ITEM="orderitem";
    private OrderItem item;
    private int status;
    private String oid;
    //private ApiProxy mApiProxy=new ApiProxy(this);


    public static void start(Context context, String email) {
        context.startActivity(new Intent(context, PaySucActivity.class));
    }

    public static void start(Context context, String email,OrderItem orderItem,int status) {
        Intent intent=new Intent(context,PaySucActivity.class);
        intent.putExtra("email",email);
        intent.putExtra("status",status);
        intent.putExtra("orderItem",orderItem);
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
            item= (OrderItem) getIntent().getSerializableExtra("orderItem");
            oid = item.getOid();
            status = getIntent().getIntExtra("status", -1);
        super.onCreate(savedInstanceState, R.layout.ct_order_pay_suc);

    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        paySucDesc.setText(getString(R.string.pay_operation_pay_feedback_suc_desc,item.getUserNick()));
        if (item != null) {
            Intent intent = new Intent(OrderItem.ORDER_STATUS_CHANGED_ACTION);
            intent.putExtra(OrderItem.KEY_ORDER_ID, item.getOid());
            sendBroadcast(intent);
        }
        CtUserInfo userInfo = LoginInstance.getInstance().getUserInfo();
        if (userInfo != null) {
            InputTime inputTime = InputTime.build(item.getServiceDate(), TimeUtil.getZeroZone());
            String formatCommonTime = TimeUtil.formatTime(inputTime, OutputTime.getInstance(OutputTime.OutputType.TYPE_COMMON_ENGLISH, TimeUtil.getZeroZone()));

            CTInformationNotificationMessage message = CtRongInfoProxy.getInstance().getCommonOrderChangeMessage(CtInfoNotificationMsgItemProvider.CtParamKey.BOOK_MSG_PAID,
                    userInfo.getNick(), inputTime.getTime(), formatCommonTime);
            CtRongInfoProxy.getInstance().sendSystemMessage(item.getTargetId(), message, null);
        }
    }

    @OnClick(R.id.back_tv)
    public void clickBack() {
        OrderFormActivity.start(PaySucActivity.this, item.getOid());
        finish();
    }

    @Override
    public boolean initCloseTopBar() {
        return false;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        CustomUiConfig config=new CustomUiConfig();
        config.mTopbarMiddleAreaName=getString(R.string.pay_operation_pay_feedback_suc);
        config.mTopbarLeftAreaName=getString(R.string.back_icon);
        return config;
    }

    @Override
    public void onTopbarLeftPress() {
        OrderFormActivity.start(PaySucActivity.this, item.getOid());
        finish();
    }

    @Override
    public void onBackPressed() {
        onTopbarLeftPress();
    }
}
